# -*- coding: utf-8 -*-

# $Id: skel.py 8 2012-09-02 01:35:34Z alexis.roda.villalonga@gmail.com $

import os

from django.core.management.base import BaseCommand
from django.core.management.base import CommandError

from dja_skel.lib import command
from dja_skel.lib.python_package import DjangoApp
from dja_skel.lib.python_package import DjangoProject


class _RealCommand(command.SkelCommand):
    entry_point_group = "dja_skel.skeleton"

    def get_root(self):
        # @FIXME: alex 2012-08-24 18:42:54 : this is a dirty hack in
        # order to make 'skel' support app and non-app
        # skeletons. Ideally the skeleton should declare that it's an
        # app skeleton than requires a DjangoApplication instance.
        if "applabel" in self._vars:
            app = DjangoApp(self._vars["applabel"])
            root = app.root
        else :
            project = DjangoProject()
            root = os.path.dirname(project.root)
        return root

    def error(self, mssg):
        raise CommandError(mssg)


class Command(BaseCommand):
    """
    """
    args = "[var=value ...]"
    help = "Instantiate an skeleton."

    option_list = BaseCommand.option_list + command.get_option_list()

    def handle(self, *args, **kwargs):
        cmd = _RealCommand(args, kwargs)
        cmd.set_output_streams(self.stdout, self.stderr)
        cmd.run()
